//
//  T3FAPolyline.swift
//  t3_map
//
//  Created by Cunhai Li on 2024/10/22.
//

import AMapNaviKit
import UIKit

class T3FMAPolyline: NSObject, T3FPolyline {

    private(set) var options: T3FPolylineOptions
    /// SDK内置的MAPolyline
    let realPolyline: T3RealPolylineProtocol
    
    weak var lineRender: MAPolylineRenderer?

    init(options: T3FPolylineOptions, realPolyline: T3RealPolylineProtocol) {
        self.options = options
        self.realPolyline = realPolyline
    }

    func update(options: T3FPolylineOptions) {
        self.options = options
        if let mulitRender = lineRender as? MAMultiTexturePolylineRenderer {
            mulitRender.setup(with: options)
        }
        lineRender?.setNeedsUpdate()
    }
}

protocol T3RealPolylineProtocol: NSObject { }

extension MAPolyline: T3RealPolylineProtocol { }

extension MAMultiTexturePolylineRenderer {
    convenience init?(from: T3FMAPolyline) {
        if let multiPolyline = from.realPolyline as? MAMultiPolyline {
            self.init(multiPolyline: multiPolyline)
            setup(with: from.options)
        } else {
            return nil
        }
    }
    
    func setup(with options: T3FPolylineOptions) {
        fillColor = options.fillColor
        strokeColor = options.strokeColor
        lineWidth = options.width
        miterLimit = options.miterLimit
        let type = options.dashType
        switch type {
        case .none:
            lineDashType = kMALineDashTypeNone
        case .square:
            lineDashType = kMALineDashTypeSquare
        case .dot:
            lineDashType = kMALineDashTypeDot
        }
        switch options.capType {
        case .butt:
            lineCapType = kMALineCapButt
        case .square:
            lineCapType = kMALineCapSquare
        case .arrow:
            lineCapType = kMALineCapArrow
        case .round:
            lineCapType = kMALineCapRound
        }
        switch options.joinType {
        case .bevel:
            lineJoinType = kMALineJoinBevel
        case .miter:
            lineJoinType = kMALineJoinMiter
        case .round:
            lineJoinType = kMALineJoinRound
        }
        if !options.lineImages.isEmpty {
            var box: [UIImage] = []
            for index in options.lineImagesIndexs {
                box.append(options.lineImages[safe: index] ?? UIImage())
            }
            strokeTextureImages = box
        }
    }
}

extension MAPolylineRenderer {
    convenience init?(with: T3FMAPolyline) {
        if let polyline = with.realPolyline as? MAPolyline {
            self.init(polyline: polyline)
            fillColor = with.options.fillColor
            strokeColor = with.options.strokeColor
            lineWidth = with.options.width
            miterLimit = with.options.miterLimit
            let type = with.options.dashType
            switch type {
            case .none:
                lineDashType = kMALineDashTypeNone
            case .square:
                lineDashType = kMALineDashTypeSquare
            case .dot:
                lineDashType = kMALineDashTypeDot
            }
            switch with.options.capType {
            case .butt:
                lineCapType = kMALineCapButt
            case .square:
                lineCapType = kMALineCapSquare
            case .arrow:
                lineCapType = kMALineCapArrow
            case .round:
                lineCapType = kMALineCapRound
            }
            switch with.options.joinType {
            case .bevel:
                lineJoinType = kMALineJoinBevel
            case .miter:
                lineJoinType = kMALineJoinMiter
            case .round:
                lineJoinType = kMALineJoinRound
            }
        } else {
            return nil
        }
    }
}
